package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.woniu.entity.PageResult;
import com.woniu.mapper.MaintainerMapper;
import com.woniu.pojo.Maintainer;
import com.woniu.service.IMaintainerServices;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.vo.MaintainerAddVo;
import com.woniu.vo.MaintainerResVo;
import com.woniu.vo.RlRoadMaintainerVo;
import com.woniu.vo.StreetVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MaintainerServicesImpl extends ServiceImpl<MaintainerMapper,Maintainer> implements IMaintainerServices {
    @Resource
    private MaintainerMapper maintainerMapper;


    @Override
    /**
     * 条件分页查询
     * */
    public PageResult searchByPage(Integer currentPage, Integer pageSize, Maintainer maintainer) {
        if(currentPage < 1 || currentPage == null){
            currentPage = 1;
        }
        if(pageSize < 1 || pageSize > 10 || pageSize == null){
            pageSize = 5;
        }
        //组装分页条件
        Page<Maintainer> page = new Page<>(currentPage,pageSize);
        QueryWrapper wrapper = new QueryWrapper();
        if(maintainer.getName() != null && maintainer.getName().length() > 0){
            wrapper.like("name",maintainer.getName());
        }
        //手机号应该是要能模糊查的，当然可以改。。
        if(maintainer.getPhone() != null && maintainer.getPhone().length() > 0){
            wrapper.like("phone",maintainer.getPhone());
        }
        if(maintainer.getStatus() != null && maintainer.getStatus().length() > 0){
            wrapper.eq("status",maintainer.getStatus());
        }
        IPage<Maintainer> maintainerIPage = maintainerMapper.selectPage(page, wrapper);
        //查到东西了，我还要组装一条管辖路段进去
        List<Maintainer> maintainerList = maintainerIPage.getRecords();
        List<MaintainerResVo> maintainerResVoList = new ArrayList<>();
        for (Maintainer  maintainers: maintainerList) {
            MaintainerResVo maintainerResVo = new MaintainerResVo();
            BeanUtils.copyProperties(maintainers,maintainerResVo);
            List<String> streetName =  maintainerMapper.selectStreetNameById(maintainers.getId());
            //我查到的全是路i段的名字，但是我要给前端的不是集合，而是单纯的一条字段。所以封装一下
            String street = "";
            for (String s : streetName) {
                street = street + s+",";
            }
            maintainerResVo.setStreet(street);
            maintainerResVoList.add(maintainerResVo);
        }
        return new PageResult(maintainerIPage.getTotal(),maintainerResVoList);
    }

    @Override
    /**
     * 查询所有的街道
     * */
    public List<StreetVo> searchAllStreet() {
        List<StreetVo> voList = maintainerMapper.selectStreetList();
        return voList;
    }

    @Override
    /**
     * 添加的方法，穿过来的是个vo
     * */
    public Boolean add(MaintainerAddVo maintainerAddVo) {
        String name = maintainerAddVo.getName();
        //名字非空校验
        if(name == null || name.length() < 1){
            return false;
        }
        String phone = maintainerAddVo.getPhone();
        //电话非空
        if(phone == null || phone.length() < 1){
            return false;
        }
       //后端的电话正则校验？
        String status = maintainerAddVo.getStatus();
        //状态非空
        if(status == null || status.length() < 1){
            return false;
        }
        String telecomGroup = maintainerAddVo.getTelecomGroup();
        //分组非空
        if(telecomGroup == null || telecomGroup.length() < 1){
            return false;
        }
        List<Long> street = maintainerAddVo.getStreet();
        //管辖非空
        if(street.size() < 1){
            return false;
        }
        String password = maintainerAddVo.getPassword();
        //密码非空
        if(password == null || password.length() < 1){
            return false;
        }
        Maintainer maintainer = new Maintainer();
        //用的是mp的方法，就的按mp的规矩来，传一个常规的对象，分批去存
        BeanUtils.copyProperties(maintainerAddVo,maintainer);
        int insert = maintainerMapper.insert(maintainer);
        //前端传回来的vo里，街道信息穿过的是个id的列表，所以要便利这个列表，并且
        //和这个运维的id组装成一个中间表对象，再保存到中间表去
        for (Long streetId : street) {
            RlRoadMaintainerVo rlRoadMaintainerVo = new RlRoadMaintainerVo();
            rlRoadMaintainerVo.setRoadId(streetId);
            rlRoadMaintainerVo.setMaintainerId(maintainer.getId());
            rlRoadMaintainerVo.setCreateTime(new Date());
            rlRoadMaintainerVo.setUpdateTime(new Date());
            maintainerMapper.addRlRoadMaintainer(rlRoadMaintainerVo);
        }
        return insert > 0 ? true:false;
    }

    @Override
    /**
     * 根据id查的方法
     * */
    public MaintainerAddVo searchById(Long id) {
        //我要查两件事，一是运维人员，二是要把街道查出来
        Maintainer maintainer = maintainerMapper.selectById(id);
        List<StreetVo> streetVos = maintainerMapper.selectStreetByMaintainerId(id);
        //吧查出来的封装到vo就行了
        MaintainerAddVo maintainerAddVo = new MaintainerAddVo();
        BeanUtils.copyProperties(maintainer,maintainerAddVo);
        //我返回前端的vo要的是路段id的集合，所以遍历封装一下
        List<Long> streetId = new ArrayList<>();
        for (StreetVo streetVo : streetVos) {
            streetId.add(streetVo.getId());
        }
        maintainerAddVo.setStreet(streetId);
        return maintainerAddVo;
    }

    @Override
    /**
    * 修改的方法
    * */
    public Boolean modify(MaintainerAddVo maintainerAddVo) {
        //先来一个常规的非空校验吧，
        String name = maintainerAddVo.getName();
        //名字非空校验
        if(name == null || name.length() < 1){
            return false;
        }
        String phone = maintainerAddVo.getPhone();
        //电话非空
        if(phone == null || phone.length() < 1){
            return false;
        }
        //后端的电话正则校验？
        String status = maintainerAddVo.getStatus();
        //状态非空
        if(status == null || status.length() < 1){
            return false;
        }
        String telecomGroup = maintainerAddVo.getTelecomGroup();
        //分组非空
        if(telecomGroup == null || telecomGroup.length() < 1){
            return false;
        }
        List<Long> street = maintainerAddVo.getStreet();
        //管辖非空
        if(street.size() < 1){
            return false;
        }
        String password = maintainerAddVo.getPassword();
        //密码非空
        if(password == null || password.length() < 1){
            return false;
        }
        Maintainer maintainer = new Maintainer();
        //修改我要用系统的方法的话，那我就要传一个运维实体过去，而不是vo，所以定义一个空的，吧数转移过去就行
        BeanUtils.copyProperties(maintainerAddVo,maintainer);
        int i = maintainerMapper.updateById(maintainer);
        //这个地方，要先把该运维原来的关系表里的数据干掉，在新增
        maintainerMapper.deleteRelationByMaintainerId(maintainerAddVo.getId());
        //我拿到的只有街道的id表，所以我要表里这个集合，再一条一条加到中间表里
        for (Long streetId : street) {
            RlRoadMaintainerVo rlRoadMaintainerVo = new RlRoadMaintainerVo();
            rlRoadMaintainerVo.setRoadId(streetId);
            rlRoadMaintainerVo.setMaintainerId(maintainer.getId());
            rlRoadMaintainerVo.setCreateTime(new Date());
            rlRoadMaintainerVo.setUpdateTime(new Date());
            //吧组装好的中间表对象加到中间表去
            maintainerMapper.addRlRoadMaintainer(rlRoadMaintainerVo);
        }
        return i > 0 ?true:false;
    }

    @Override
    /**
     * 删除的放啊
     * */
    public Boolean removesById(Long id) {
        //删除要干两件事，通过这个id去吧运维表里的数据干掉，
        int i = maintainerMapper.deleteById(id);
        //还要吧中间表的数据干掉
        maintainerMapper.deleteRelationByMaintainerId(id);
        return i > 0?true:false;
    }

    @Override
    /**
     * 禁用的方法
     * */
    public Boolean stopMaintainer(Long id) {
        int i = maintainerMapper.updateStatusToStopById(id);
        return i > 0 ? true : false ;
    }

    @Override
    public Boolean useMaintainer(Long id) {
        //调用方法去改status的值
        int i = maintainerMapper.updateStatusToUseById(id);
        return i > 0 ? true : false ;
    }
}
